﻿using PFMCandidate;
using PFMConfigurations;
using PFMFinders;
using PFMGlobals;
using PFMHandlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PFMKernels
{
   public  class PFMTask:ITask
    {
        protected List<IHandler> o_Handlers = null;
        public virtual bool Execute(PFMConfigurations.Schedule aSchedule,PFMConfigurations.Target aTarget)
        {
            return true;
        }
        protected virtual List<IHandler>CreateHandlersFromTarget(Target aTarget)
        {
            List<IHandler> result = new List<IHandler>();
            foreach (string s  in aTarget.Handler)
                result.Add(PFMHandlerFactory.CreateHandler(s));
            result.Add(PFMHandlerFactory.CreateHandler(aTarget.Destination));

            return result;

        }
        protected void BroadCastHandlers(PFMCandidate.Candidate aCandidate, List<IHandler> aHandlers)
        {
            byte[] baTarget = null;
            foreach (IHandler handler in aHandlers)
                baTarget = handler.Perform(aCandidate, baTarget);
        }
        protected void ApplyHandlersToTarget(Target aTarget,List<IHandler>aHandlers)
        {
            PFMFileFinder aFinder = PFMFinderFactory.CreateFinder(PFMDefinitions.FILEFINDER, aTarget);
            Candidate aCandidate = null;
            if (aFinder == null) return;
            while(true)
            {
                aCandidate = aFinder.FindNext();
                if (aCandidate != null) BroadCastHandlers(aCandidate, aHandlers);
                else break;
            }
        }
    }
}
